class Baddy
  
  attr_accessor :badguy_shape, :badguy_body, :hp, :x, :y

  def initialize(window)
    @window = window
    # grab images
    @badguy_image = Gosu::Image.new(window, "media/mouth.bmp", false)
    @exp_anim = Gosu::Image::load_tiles(window, "media/explosion.PNG", 25, 25, false)
    
    #create body & shape
    @badguy_body = CP::Body.new(10.0, 150.0)
    badguy_shape_array = [ CP::Vec2.new(-30.0, -50.0),
                           CP::Vec2.new(-30.0, 50.0),
                           CP::Vec2.new(30.0, 50.0),
                           CP::Vec2.new(30.0, -50.0) ]
    @badguy_shape = CP::Shape::Poly.new(@badguy_body, badguy_shape_array, CP::Vec2.new(0,0))
    @badguy_shape.collision_type = :enemy
    
    # angle in radians; faces towards top of screen
    @badguy_shape.body.a = (3*Math::PI/2.0)
    @badguy_shape.body.v = CP::Vec2.new(rand(50.0), rand(150.0)+100 )
    # initialize attributes
    @hp = 50 + ($LEVEL/2)
    #@weaponPower = 1
    
    # make x and y referencable
    @x = @badguy_shape.body.p.to_a[0]
    @y = @badguy_shape.body.p.to_a[1]
    
    $space.add_body(@badguy_body)
    $space.add_shape(@badguy_shape)
  end
  
  #blow up when full of ketchup
  def explode
    @exp_anim = Gosu::Image::load_tiles(@window, "media/Explosion.png", 25, 25, false)
    img = @exp_anim[Gosu::milliseconds / 100 % @exp_anim.size];
    img.draw(@badguy_shape.body.p.to_a[0] - img.width / 2.0,
             @badguy_shape.body.p.to_a[1] - img.height / 2.0,
             ZOrder::Baddy, 1, 1, 0xffffffff, :default)
  end
  
  # Directly set the position of badguy
  def warp(vect)
    @badguy_shape.body.p = vect
  end
  
  def movement
    state = rand(50)
    case state
    when 1
      @badguy_shape.body.apply_impulse(-(@badguy_shape.body.t.radians_to_vec2 * (800.0/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
    when 2
      @badguy_shape.body.apply_impulse((@badguy_shape.body.t.radians_to_vec2 * (800.0/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
    when 3
      @badguy_shape.body.apply_impulse(-(@badguy_shape.body.a.radians_to_vec2 * (800.0/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
    when 4
      @badguy_shape.body.apply_impulse((@badguy_shape.body.a.radians_to_vec2 * (800.0/SUBSTEPS)), CP::Vec2.new(0.0, 0.0))
    end
  end
  
  
  # keep on screen
  def validate_position
    if (badguy_shape.body.p.x.round >= SCREEN_WIDTH)
      warp(CP::Vec2.new(SCREEN_WIDTH, badguy_shape.body.p.y.round)) 
    end
    
    if (badguy_shape.body.p.x.round <= 0)
      warp(CP::Vec2.new(0, badguy_shape.body.p.y.round))
    end
    
    if (badguy_shape.body.p.y.round >= SCREEN_HEIGHT)
      warp(CP::Vec2.new(badguy_shape.body.p.x.round, SCREEN_HEIGHT))
    end
    
    if (badguy_shape.body.p.y.round <= 0)
      warp(CP::Vec2.new(badguy_shape.body.p.x.round, 0))
    end
  end
  
  def draw
    @badguy_image.draw_rot(@badguy_shape.body.p.x, @badguy_shape.body.p.y, ZOrder::Baddy, @badguy_shape.body.a.radians_to_gosu)
  end
  
end